{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-07-30T14:11:29.233482Z",
     "start_time": "2019-07-30T14:11:28.086710Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RBP: 0.474, Residuals: 0.00, P@100: 0.19\n"
     ]
    }
   ],
   "source": [
    "from trectools import TrecQrel, TrecRun, TrecEval\n",
    "\n",
    "# A typical evaluation workflow\n",
    "r1 = TrecRun(\"./robust03/runs/input.aplrob03a.gz\")\n",
    "r1.topics()[:5] # Shows the first 5 topics: 601, 602, 603, 604, 605\n",
    "\n",
    "qrels = TrecQrel(\"./robust03/qrel/robust03_qrels.txt\")\n",
    "\n",
    "te = TrecEval(r1, qrels)\n",
    "rbp, residuals = te.get_rbp()           # RBP: 0.474, Residuals: 0.001\n",
    "p100 = te.get_precision(depth=100)     # P@100: 0.186\n",
    "\n",
    "print(\"RBP: %.3f, Residuals: %.2f, P@100: %.2f\" % (rbp, residuals, p100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-07-30T14:11:29.568140Z",
     "start_time": "2019-07-30T14:11:29.235771Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Average number of documents judged among top 10: 9.99, among top 1000: 481.39\n"
     ]
    }
   ],
   "source": [
    "# Check if documents retrieved by the system were judged:\n",
    "cover10 = r1.get_mean_coverage(qrels, topX=10)   # 9.99\n",
    "cover1000 = r1.get_mean_coverage(qrels, topX=1000) # 481.390 \n",
    "# On average for system 'input.aplrob03a' participating in robust03, 480 documents out of 1000 were judged.\n",
    "\n",
    "print(\"Average number of documents judged among top 10: %.2f, among top 1000: %.2f\" % (cover10, cover1000))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-07-30T14:11:29.818407Z",
     "start_time": "2019-07-30T14:11:29.569574Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Average number of documents retrieved by Run1 and Run2: 3.640\n"
     ]
    }
   ],
   "source": [
    "# Loads another run\n",
    "r2 = TrecRun(\"./robust03/runs/input.UIUC03Rd1.gz\")\n",
    "\n",
    "# Check how many documents, on average, in the top 10 of r1 were retrieved in the top 10 of r2\n",
    "retrievedBYBothRuns = r1.check_run_coverage(r2, topX=10) # 3.64\n",
    "\n",
    "print(\"Average number of documents retrieved by Run1 and Run2: %.3f\" % (retrievedBYBothRuns))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-07-30T14:11:40.135812Z",
     "start_time": "2019-07-30T14:11:29.819619Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P-value for wrt P@10 between r1 and r2: 0.017\n"
     ]
    }
   ],
   "source": [
    "# Evaluates r1 and r2 using all implemented evaluation metrics\n",
    "result_r1 = r1.evaluate_run(qrels, per_query=True) \n",
    "result_r2 = r2.evaluate_run(qrels, per_query=True)\n",
    "\n",
    "# Inspect for statistically significant differences between the two runs for  P_10 using two-tailed Student t-test\n",
    "pvalue = result_r1.compare_with(result_r2, metric=\"P_10\") # pvalue: 0.0167 \n",
    "\n",
    "print(\"P-value for wrt P@10 between r1 and r2: %.3f\" % (pvalue[1]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
